package com.code_g.dao;

import com.code_g.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserDao extends JpaRepository<User,Integer>, JpaSpecificationExecutor<User> {

    //通过username查询user
    @Query("from User where username = ?")
    User findByUsername(String username);

    @Query("from User where username = ? and id = ?")
    User findByUsernameAndId(String name,int id);

    //更新操作
    //问号后的数字是参数的索引位置
    //更新/删除操作需要使用@Modifying注解
    @Query("update User set password = ?2 where id = ?1")
    @Modifying
    void updateUser(int id,String password);

    //sql语句查询所有
    @Query(value = "select * from user",nativeQuery = true)
    List<User> findAllUser();

    //方法命名规则查询
    User findById(int id);
}
